package main.java.person.fjsp.algorithm.ga;

import main.java.person.fjsp.algorithm.ga.entity.Individual;
import main.java.person.fjsp.algorithm.ga.utils.GAUtils;
import main.java.person.fjsp.common.entity.DataManager;
import main.java.person.fjsp.common.entity.Machine;
import main.java.person.fjsp.common.entity.ScheduleResult;
import main.java.person.fjsp.common.utils.ProcessDataUtils;

import java.util.Comparator;

public class GATest {

    public static void main(String[] args) throws Exception {
        long beginTime = System.currentTimeMillis();
        String dataName="MK";
        String dataNumber = "01";
        DataManager dataManager = new ProcessDataUtils().readFileToJobList("data/"+dataName+ dataNumber + ".fjs");
        GAUtils gaUtils = new GAUtils();
        GASolver gaSolver = new GASolver(3000, 1050,  0.95, 0.1, 200, 50);
        gaSolver.setMaxCalTime(360000L);
        Individual solve = gaSolver.solve(dataManager);
        System.out.println("==========="+dataName+dataNumber+"=========");
        ScheduleResult scheduleResult = gaUtils.getScheduleResult(solve.getCsCode(), solve.getOsCode(), dataManager);
        gaUtils.checkScheduleResult(scheduleResult, dataManager);
        System.out.println("最大适应值：" + solve.getFitness());
        int completeTime = scheduleResult.getMachines().stream().max(Comparator.comparing(Machine::getCompleteTime)).orElseThrow().getCompleteTime();
        System.out.println("最大完工时间：" + completeTime);
        new ProcessDataUtils().writeSchedule("result/GA-"+dataName + dataNumber + ".txt", scheduleResult);
        System.out.println("计算时间"+(System.currentTimeMillis() - beginTime));
    }
}
